package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.ConfigurationInfoDTO;
import com.night.icm.service.base.service.ConfigurationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * SystemConfigurationController
 *
 * @author Liao ZhiYong
 * @date 2020/1/24
 */
@Api(tags = {"全局参数配置"})
@RestController
@RequestMapping("/api/icm-system-configuration")
public class SystemConfigurationController {

    private final ConfigurationInfoService configurationInfoService;

    @Autowired
    public SystemConfigurationController(ConfigurationInfoService configurationInfoService) {
        this.configurationInfoService = configurationInfoService;
    }

    @ApiOperation(value = "分页查询全局参数配置信息")
    @GetMapping(value = "")
    public PageDataDTO<ConfigurationInfoDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "是否可用") @RequestParam(value = "available", required = false) Boolean available
    ) {
        return this.configurationInfoService.findOnePage(page, size, sort, name, available);
    }

    @ApiOperation(value = "通过id查询全局参数配置信息")
    @GetMapping(value = "/{id}")
    public ConfigurationInfoDTO findById(@ApiParam(value = "全局参数配置Id", required = true) @PathVariable(name = "id") String id) {
        return this.configurationInfoService.findById(id);
    }

    @ApiOperation(value = "查找所有全局参数配置信息")
    @GetMapping(value = "/all")
    public List<ConfigurationInfoDTO> findAll() {
        return this.configurationInfoService.findAll();
    }

    @ApiOperation(value = "添加全局参数配置信息")
    @PostMapping(value = "")
    public ConfigurationInfoDTO create(@ApiParam(value = "全局参数配置信息", required = true) @RequestBody @Validated ConfigurationInfoDTO configurationInfoDTO) {
        return this.configurationInfoService.create(configurationInfoDTO);
    }

    @ApiOperation(value = "根据id删除全局参数配置")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "全局参数配置Id", required = true) @PathVariable(name = "id") String id) {
        this.configurationInfoService.delete(id);
    }

    @ApiOperation(value = "更新全局参数配置信息")
    @PutMapping(value = "/{id}")
    public ConfigurationInfoDTO update(
            @ApiParam(value = "全局参数配置id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "全局参数配置信息", required = true) @RequestBody @Validated ConfigurationInfoDTO configurationInfoDTO
    ) {
        return this.configurationInfoService.update(id, configurationInfoDTO);
    }

    @ApiOperation(value = "检查配置名称唯一性")
    @GetMapping(value = "/only")
    public SimpleDataDTO<Boolean> checkConfigurationName(
            @ApiParam(value = "配置名称", required = true) @RequestParam(value = "name") String name) {
        return this.configurationInfoService.checkConfigurationName(name);
    }
}